package com.data.util.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.util.StringUtils;

/**
 * @author helele
 * @date 2021/5/31
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Result {
    private int code;
    private String msg;
    private Object data;

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Result success() {
        return new Result(ResultValue.DEFAULT_SUCCESS_CODE, ResultValue.DEFAULT_SUCCESS_MESSAGE);
    }

    public static Result success(String msg) {
        if (StringUtils.isEmpty(msg)) {
            return success();
        }
        return new Result(ResultValue.DEFAULT_SUCCESS_CODE, msg);
    }


    public static Result success(String msg, Object object) {
        if (StringUtils.isEmpty(msg)) {
            return success(object);
        }
        return new Result(ResultValue.DEFAULT_SUCCESS_CODE, msg, object);
    }

    public static Result success(Object object) {
        return new Result(ResultValue.DEFAULT_SUCCESS_CODE, ResultValue.DEFAULT_SUCCESS_MESSAGE, object);
    }

    public static Result failure(String msg) {
        if (StringUtils.isEmpty(msg)) {
            return failure();
        }
        return new Result(ResultValue.DEFAULT_FAILURE_CODE, msg);
    }

    public static Result failure() {

        return new Result(ResultValue.DEFAULT_FAILURE_CODE, ResultValue.DEFAULT_FAILURE_MESSAGE);
    }

    /**
     * 参数异常
     */
    public static Result paramErr() {
        return new Result(ResultValue.DEFAULT_FAILURE_CODE, ResultValue.DEFAULT_PARAM_ERROR_MESSAGE);
    }

    public static Result paramErr(String msg) {
        if (StringUtils.isEmpty(msg)) {
            return paramErr();
        }
        return new Result(ResultValue.DEFAULT_FAILURE_CODE, msg);
    }

}














